<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AppAction
 *
 * @author Administrator
 */
import('@.Filter.SecurityAction');
import('@.Service.AppService');
import('@.Service.CommentService');
import('@.Service.FileService');
import('ORG.Util.Page');
import("@.ORG.Net.Keditor");

class AppAction extends SecurityAction {

    function appAddPage() {

        $ke = new Keditor();
        $ke->id = "app_content"; //指定textarea的id
        $ke->form = "frm_app_add"; //指定form的id，默认为from1
        $ke->afterCreate = "ctrlenter"; //定义创建后执行函数，ctrlenter表示ctrl+enter提交表单。
//        $ke->imageUploadJson="http://localhost/itunesapp/index.php/File/kindeditorUpload";//上传图片的php处理文件默认为/index.php/Keditor/upload
        $keshow = $ke->show(); //生成js代码
        $this->assign("keshow", $keshow);


        $this->assign('content', 'appAdd');
        $this->display('Admin:global');
    }

    function appAdd() {
        //需要上传App  Icon
        $app = D('app')->create();
        $typeId = $_POST['type_id'];

        if (!empty($typeId)) {
            $app['typesIdList'] = split(',', $typeId);
        }
        $appService = new AppService();

        $id = $_POST['id'];
        if (!empty($id)) {
            $app['release'] = empty($_POST['release']) ? 0 : 1;
            $appService->save($app);

            $this->redirect('App/app');

            return;
        }




//        $fileId = $_POST['file_id'];
//        if (!empty($fileId)) {
//            $app['fileIdList'] = split(',', $fileId);
//        }

        $appService->insert($app);

        $this->redirect('App/app');
    }

    /**
     * 操作 精品推荐  状态
     */
    function appFine() {
        $id = $_POST['id'];
        $fine = $_POST['fine'];

        if (empty($id) || '' == $fine) {
            $this->ajaxReturn(null, '参数未传递', 0);
            return;
        }
        $appService = new AppService();
        $app = $appService->findAppById($id);
        if (empty($app)) {
            $this->ajaxReturn(null, '当前数据不存在，请重新刷新数据', 0);
            return;
        }

        $app['fine'] = $fine;

        $lastId = $appService->updateApp($app);
        if (0 < $lastId) {
            $this->ajaxReturn($fine, '数据已成功更新', 1);
        } else {
            $this->ajaxReturn(null, '数据更新失败', 0);
        }
    }

    /**
     * app 管理首页
     */
    function app() {
        $type = empty($_REQUEST['type']) ? 'app' : $_REQUEST['type'];
        $release = $_REQUEST['release'];

        $appService = new AppService();
        $typeName = $appService->getAppTypeName($type);
        $appName = $_REQUEST['app_name'];


//        dump($typeName);
//        $count = $appService->countAppByTypeName($typeName, $appName, $release);
        $count = $appService->countApp1($appName, $release);

        //dump(D('app')->getLastSql());
        $page = new Page($count, 10);

        $map['type'] = $type;
        $map['app_name'] = $appName;
        $map['release'] = $release;
        foreach ($map as $key => $val) {
            $page->parameter .= "$key=" . urlencode($val) . "&";
        }
        $show = $page->show();
//        $list = $appService->listAppByTypeName($typeName, $appName, $page->firstRow, $page->listRows, 'datetime desc', $release);
        $list = $appService->listApp1($appName, $page->firstRow . ',' . $page->listRows, $release);


        $this->assign('release', $release);
        $this->assign('typeName', $typeName);
        $this->assign('type', $type);
        $this->assign('appName', $appName);

        $this->assign('list', $list);
        $this->assign('show', $show);

        $this->assign('content', 'app');
        $this->display('Admin:global');
    }

    //评论
    function appComment() {
        $id = $_REQUEST['id'];
        $appService = new AppService();
        $commentService = new CommentService();

        $count = $commentService->countCommentByAId($id);

        $page = new Page($count, 10);

        $show = $page->show();
        $field = 'u.nick_name, comment.*';
        $list = $commentService->listCommentByAId($id, $field, $page->firstRow . ',' . $page->listRows);

        $this->assign('app_id', $id);
        $this->assign('list', $list);
        $this->assign('show', $show);
        $this->assign('content', 'app_comment');
        $this->display('Admin:global');
    }

    function appCommentReply() {
        $id = $_REQUEST['id'];
        $commentService = new CommentService();

        $comm = $commentService->findAppCommentById($id);

        $this->assign('app_id', $_REQUEST['app_id']);
        $this->assign('comm', $comm);
        $this->assign('content', 'app_comment_reply');
        $this->display('Admin:global');
    }

    function saveAppCommentReply() {
        $id = $_REQUEST['id'];
        $reply = $_REQUEST['reply'];

        $commentService = new CommentService();

        $comm = $commentService->findCommentById($id);
        $comm['reply'] = $_REQUEST['reply'];
        $comm['reply_datetime'] = ConstantUtil::date();

        $commentService->update($comm);
//        dump(D('comment')->getLastSql());
//        return;
        $this->redirect('/App/appComment/id/' . $_REQUEST['app_id']);
    }

    function deleteAppComment() {
        $id = $_REQUEST['id'];
        $commentService = new CommentService();
        $commentService->delete($id);

        $this->redirect('/App/appComment/id/' . $_REQUEST['app_id']);
    }

    /**
     * 自动抓取App的修改页面
     */
    function findAppUpdatePage() {
        $id = $_POST['id'];
        $type = $_POST['type'];

        $this->assign('type', $type);

        $appService = new AppService();

        $app = $appService->findAppAndTypeById($id);

        $this->assign('app', $app);
        import('@.Util.KeditorUtil');

        $keditor = new KeditorUtil();
        $this->assign("keshow", $keditor->getKeditor('app_content', 'frm_app'));



        $this->assign('content', 'appAdd');
        $this->display('Admin:global');
    }

    /**
     * 自动抓取数据修改后保存
     */
    function appAutoLoadSave() {
        $id = $_POST['id'];
        $app['name'] = $_POST['name'];
        $app['release'] = empty($_POST['release']) ? 0 : 1;
        $app['content'] = $_POST['content'];

        $appService = new AppService();

        $lastId = $appService->updateAutoLoad($id, $app);

        $this->redirect('App/app/type/' . $_POST['type']);
    }

    /**
     * 删除
     */
    function appDelete() {
        $id = $_POST['id'];
        $appService = new AppService();

        $lastId = $appService->deleteApp($id);

        $this->redirect('App/app/type/' . $_POST['type']);
    }

}

?>
